package safeCommitter.core.view.main;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import safeCommitter.test.executor.common.TestResultType;
import safeCommitter.test.report.ISCClassMethodTestResult;
import safeCommitter.test.report.ISCTestResult;
import safeCommitter.test.report.RootClazzResult;
import safeCommitter.test.report.SCClazzTestResult;
import safeCommitter.test.report.SCTestResult;

public class SCLabelProvider extends LabelProvider implements
		ITableLabelProvider {

	private final Image[] images;

	public SCLabelProvider() {
		images = new Image[3];
		images[0] = SCViewUtils.createImage("icons/suiteok.gif");
		images[1] = SCViewUtils.createImage("icons/suitefail.gif");
		images[2] = SCViewUtils.createImage("icons/suiteskip.gif");

	}

	public String getColumnText(final Object element, final int columnIndex) {
		if (element instanceof SCTestResult) {
			return handleLeaf(element, columnIndex);
		} else if (element instanceof SCClazzTestResult) {
			return handleNode(element, columnIndex);
		}
		return null;

	}

	private String handleNode(final Object element, final int columnIndex) {
		ISCClassMethodTestResult result = (ISCClassMethodTestResult) element;
		switch (columnIndex) {
		case 0:
			return result.getClassName();
		case 1:
			return result.getTimeSpent();
		case 2:
			return "";
			// return result.getChildren().get(0).getTestType().toString();
		}
		return null;
	}

	private String handleLeaf(final Object element, final int columnIndex) {
		SCTestResult result = (SCTestResult) element;
		switch (columnIndex) {
		case 0:
			return result.getTestName();
		case 1:
			return result.getTimeSpent();
		case 2:
			return "";
		}
		return null;
	}

	public Image getColumnImage(final Object element, final int columnIndex) {
		if (columnIndex != 0 || element instanceof RootClazzResult) {
			return null;
		}
		ISCTestResult result = (ISCTestResult) element;
		int index = getImageIndex(result.getStatus());
		return images[index];

	}

	private int getImageIndex(final TestResultType status) {
		if (TestResultType.SUCCESS.equals(status)) {
			return 0;
		}
		if (TestResultType.FAILING.equals(status)) {
			return 1;
		} else
			return 2;

	}

	@Override
	public void dispose() {
		for (int i = 0; i < images.length; i++)
			images[i].dispose();
	}

}
